package com.example.x6.configurationmaintenance.Module;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import com.example.x6.configurationmaintenance.Interface.LogPrecess.OnLogProcessChangeListener;
import com.example.x6.configurationmaintenance.Utils.LogOperationUtil;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.Vector;

public class LogOperationManager {
    private static LogOperationManager logOperationManager;
    private final String TAG = getClass().getSimpleName();
    private Context context;
    private final String continualLogDir = "/sdcard/MC_CONFIG_LOG/continualLog/";
    private final String continualLogPackDir = "/sdcard/MC_CONFIG_LOG/continualLogPack/";
    private final String empty = "-1";
    private final String isLogAlwaysKey = "isLogAlwaysKey";
    private LogOperationUtil logOperationUtil;
    private final String logPidFile = "logPidFile";
    private final String mainPidKey = "mainPidKey";
    private OnLogProcessChangeListener onLogProcessChangeListener;
    private final String radioPidKey = "radioPidKey";
    private SharedPreferences sharedPreferences;
    private final String tempLogDir = "/sdcard/MC_CONFIG_LOG/tempLog/";
    private final String tempLogPackDir = "/sdcard/MC_CONFIG_LOG/tempLogPack/";

    public static synchronized void initializeInstance(Context context2) {
        synchronized (LogOperationManager.class) {
            if (logOperationManager == null) {
                logOperationManager = new LogOperationManager(context2);
            }
        }
    }

    public static synchronized LogOperationManager getInstance() {
        LogOperationManager logOperationManager2;
        synchronized (LogOperationManager.class) {
            if (logOperationManager == null) {
                throw new IllegalStateException("LogOperationManager is not initalized ,call initializeInstance(Context context) first");
            }
            logOperationManager2 = logOperationManager;
        }
        return logOperationManager2;
    }

    private LogOperationManager(Context context2) {
        this.context = context2;
        this.logOperationUtil = new LogOperationUtil();
        this.sharedPreferences = context2.getSharedPreferences("logPidFile", 0);
    }

    public void setOnLogProcessChangeListener(OnLogProcessChangeListener onLogProcessChangeListener2) {
        this.onLogProcessChangeListener = onLogProcessChangeListener2;
    }

    public void startBackgroundLogProcess(final String path, final String timeString) {
        new Thread(new Runnable() {
            /* class com.example.x6.configurationmaintenance.Module.LogOperationManager.AnonymousClass1 */

            public void run() {
                LogOperationManager.this.startSaveLogBackground(path, timeString);
            }
        }).start();
    }

    public void stopBackgroundLogProcess() {
        new Thread(new Runnable() {
            /* class com.example.x6.configurationmaintenance.Module.LogOperationManager.AnonymousClass2 */

            public void run() {
                LogOperationManager.this.stopSaveLogBackgroungd();
            }
        }).start();
    }

    public void startFrontLogProcess(final String path, final String timeString) {
        new Thread(new Runnable() {
            /* class com.example.x6.configurationmaintenance.Module.LogOperationManager.AnonymousClass3 */

            public void run() {
                LogOperationManager.this.saveLogSingle(path, timeString);
            }
        }).start();
    }

    public synchronized void enableBackgroundProcess() {
        putIsAlwaysLog(true);
    }

    public synchronized void disableBackgroundProcess() {
        putIsAlwaysLog(false);
    }

    public synchronized boolean isEnableBackgroundProcess() {
        return getIsAlwaysLog();
    }

    public synchronized void cleanLogProcessPID() {
        putLogRadioPID("-1");
        putLogMainPID("-1");
        if (isEnableBackgroundProcess()) {
            startBackgroundLogProcess("/sdcard/MC_CONFIG_LOG/continualLog/", new SimpleDateFormat("MMdd_HHmmss").format(Long.valueOf(System.currentTimeMillis())));
        }
    }

    public synchronized void startPackTempLog() {
        new Thread(new Runnable() {
            /* class com.example.x6.configurationmaintenance.Module.LogOperationManager.AnonymousClass4 */

            public void run() {
                LogOperationManager.this._packTempLog();
            }
        }).start();
    }

    public synchronized void startPackContinualLog() {
        new Thread(new Runnable() {
            /* class com.example.x6.configurationmaintenance.Module.LogOperationManager.AnonymousClass5 */

            public void run() {
                LogOperationManager.this._packContinualLog();
            }
        }).start();
    }

    public synchronized void startClearTempLog() {
        new Thread(new Runnable() {
            /* class com.example.x6.configurationmaintenance.Module.LogOperationManager.AnonymousClass6 */

            public void run() {
                LogOperationManager.this._clearTempLog();
            }
        }).start();
    }

    public synchronized void startClearTempLogPack() {
        new Thread(new Runnable() {
            /* class com.example.x6.configurationmaintenance.Module.LogOperationManager.AnonymousClass7 */

            public void run() {
                LogOperationManager.this._clearTempLogPack();
            }
        }).start();
    }

    public synchronized void startClearContinualLog() {
        new Thread(new Runnable() {
            /* class com.example.x6.configurationmaintenance.Module.LogOperationManager.AnonymousClass8 */

            public void run() {
                LogOperationManager.this._clearContinualLog();
            }
        }).start();
    }

    public synchronized void startClearContinualLgoPack() {
        new Thread(new Runnable() {
            /* class com.example.x6.configurationmaintenance.Module.LogOperationManager.AnonymousClass9 */

            public void run() {
                LogOperationManager.this._clearContimualLogPack();
            }
        }).start();
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void _packTempLog() {
        this.onLogProcessChangeListener.onTempLogPackFinished(this.logOperationUtil.packTempLog());
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void _packContinualLog() {
        this.onLogProcessChangeListener.onContinualLogPackFinished(this.logOperationUtil.packContinualLog());
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void _clearTempLog() {
        this.onLogProcessChangeListener.onTempLogClearFinished(this.logOperationUtil.clearTempLog());
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void _clearTempLogPack() {
        this.onLogProcessChangeListener.onTempLogPackClearFinished(this.logOperationUtil.clearTempLogPack());
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void _clearContinualLog() {
        this.onLogProcessChangeListener.onContinualLogClearFinished(this.logOperationUtil.clearContinualLog());
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void _clearContimualLogPack() {
        this.onLogProcessChangeListener.onContinualLogPackClearFinished(this.logOperationUtil.clearContinualLogPack());
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private boolean saveLogSingle(String path, String timeString) {
        initDir();
        boolean result = false;
        String[] files = {path + timeString + "main_log.txt", path + timeString + "radio_log.txt", path + timeString + "dmesg_log.txt"};
        if (this.logOperationUtil.logMainFront(files[0]) && this.logOperationUtil.logRadioFront(files[1]) && this.logOperationUtil.dmesgFrong(files[2])) {
            result = true;
        }
        if (this.onLogProcessChangeListener != null) {
            this.onLogProcessChangeListener.onLogFrontProcessFinished(result, files);
        }
        return result;
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private synchronized boolean stopSaveLogBackgroungd() {
        Log.d(this.TAG, "Clean All PIDS");
        disableBackgroundProcess();
        Vector<String> realRunning = this.logOperationUtil.getpids();
        if (realRunning != null) {
            Iterator<String> it = realRunning.iterator();
            while (it.hasNext()) {
                String pid = it.next();
                String str = this.TAG;
                Log.d(str, "Kill " + pid);
                this.logOperationUtil.killProcess(pid);
            }
            putLogRadioPID("-1");
            putLogMainPID("-1");
        } else {
            Log.w(this.TAG, "Nothing to kill");
        }
        if (this.onLogProcessChangeListener != null) {
            this.onLogProcessChangeListener.onLogBackgroundProcessFinished(true);
        }
        return true;
    }

    private synchronized void initDir() {
        File nonblock = new File("/sdcard/MC_CONFIG_LOG/tempLog/");
        File block = new File("/sdcard/MC_CONFIG_LOG/continualLog/");
        File nonBlockPack = new File("/sdcard/MC_CONFIG_LOG/tempLogPack/");
        File blockPack = new File("/sdcard/MC_CONFIG_LOG/continualLogPack/");
        if (!nonblock.exists()) {
            nonblock.mkdirs();
        }
        if (!block.exists()) {
            block.mkdirs();
        }
        if (!nonBlockPack.exists()) {
            nonBlockPack.mkdirs();
        }
        if (!blockPack.exists()) {
            blockPack.mkdirs();
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private synchronized void startSaveLogBackground(String path, String timeString) {
        enableBackgroundProcess();
        initDir();
        Vector<String> realRunning = this.logOperationUtil.getpids();
        if (realRunning != null) {
            if (realRunning.size() != 2) {
                Iterator<String> it = realRunning.iterator();
                while (it.hasNext()) {
                    this.logOperationUtil.killProcess(it.next());
                }
                putLogRadioPID("-1");
                putLogMainPID("-1");
            } else if ((realRunning.get(0).equals(getLogMainPID()) || realRunning.get(0).equals(getLogRadioPID())) && ((realRunning.get(1).equals(getLogMainPID()) || realRunning.get(1).equals(getLogRadioPID())) && !realRunning.get(0).equals(realRunning.get(1)))) {
                Log.w(this.TAG, "log main and log radio process is running...");
                if (this.onLogProcessChangeListener != null) {
                    this.onLogProcessChangeListener.onLogBackgroundProcessFinished(true);
                }
                return;
            } else {
                Log.e(this.TAG, "log main and log radio pid not equal save pids");
                Iterator<String> it2 = realRunning.iterator();
                while (it2.hasNext()) {
                    this.logOperationUtil.killProcess(it2.next());
                }
                putLogRadioPID("-1");
                putLogMainPID("-1");
            }
        }
        LogOperationUtil logOperationUtil2 = this.logOperationUtil;
        Vector<String> pids = logOperationUtil2.logBackup(path + timeString);
        if (pids == null || pids.size() != 2) {
            if (this.onLogProcessChangeListener != null) {
                this.onLogProcessChangeListener.onLogBackgroundProcessFinished(false);
            }
            return;
        }
        if (pids.size() == 2) {
            putLogMainPID(pids.get(0));
            putLogRadioPID(pids.get(1));
        } else if (this.onLogProcessChangeListener != null) {
            this.onLogProcessChangeListener.onContinualLogPackClearFinished(false);
        }
        LogOperationUtil logOperationUtil3 = this.logOperationUtil;
        if (logOperationUtil3.dmesgFrong(path + timeString)) {
            if (this.onLogProcessChangeListener != null) {
                this.onLogProcessChangeListener.onLogBackgroundProcessFinished(true);
            }
        } else if (this.onLogProcessChangeListener != null) {
            this.onLogProcessChangeListener.onLogBackgroundProcessFinished(false);
        }
    }

    private synchronized void putIsAlwaysLog(boolean isAlways) {
        this.sharedPreferences.edit().putBoolean("isLogAlwaysKey", isAlways).commit();
    }

    private synchronized boolean getIsAlwaysLog() {
        return this.sharedPreferences.getBoolean("isLogAlwaysKey", false);
    }

    private synchronized void putLogMainPID(String PID) {
        if (PID != null) {
            String str = this.TAG;
            Log.w(str, "PUT MAIN PID:" + PID);
            this.sharedPreferences.edit().putString("mainPidKey", PID).commit();
        }
    }

    private synchronized void putLogRadioPID(String PID) {
        if (PID != null) {
            String str = this.TAG;
            Log.w(str, "PUT RADIO PID:" + PID);
            this.sharedPreferences.edit().putString("radioPidKey", PID).commit();
        }
    }

    private synchronized String getLogMainPID() {
        String str = this.TAG;
        Log.w(str, "GET MAIN PID:" + this.sharedPreferences.getString("mainPidKey", "-1"));
        return this.sharedPreferences.getString("mainPidKey", "-1");
    }

    private synchronized String getLogRadioPID() {
        String str = this.TAG;
        Log.w(str, "GET RADIO PID:" + this.sharedPreferences.getString("radioPidKey", "-1"));
        return this.sharedPreferences.getString("radioPidKey", "-1");
    }
}
